<?php
/**
 * +----------------------------------------------------------------------
 * | [ DO WHAT YOU LOVE AND FUCK THE REST ]
 * +----------------------------------------------------------------------
 * | EMAIL: < 931274989@qq.com >
 * +----------------------------------------------------------------------
 * | WECHAT: xzc016
 * +----------------------------------------------------------------------
 * | 路由配置
 * +----------------------------------------------------------------------
 */
namespace app\admin\controller;

use app\common\model\Route as RouteModel;
use app\admin\validate\Route as RouteValidate;

use think\facade\View;

class Route extends Base
{
  // 路由列表
  public function index()
  {
    $model = new RouteModel();
    $route = $model->order('create_time desc')->paginate(20);
    View::assign('route',$route);
    return View::fetch();
  }

  // 新增修改路由
  public function routeAdd()
  {
    //获取规则id
    $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    $model = new RouteModel();
    //是正常添加操作
    if($id > 0) {
      //是修改操作
      if($this->request->isPost()) {
        //是提交操作
        $post = $this->request->post();
        //验证 
        (new RouteValidate())->goCheck();
        //验证规则是否存在
        $route = $model->where('id',$id)->find();
        if(empty($route)) {
          return error('id不正确');
        }
        if(false == $model->where('id',$id)->save($post)) {
          return error('修改失败');
        } else {
          adminLog();//写入日志
          return success('修改成功','admin/Route/index');
        }
      } else {
        //非提交操作
        $route = $model->where('id',$id)->find();
        if(!empty($route)) {
          View::assign('route',$route);
          return View::fetch();
        } else {
          return error('id不正确');
        }
      }
    } else {
      //是新增操作
      if($this->request->isPost()) {
        //是提交操作
        $post = $this->request->post();
        //验证  
        (new RouteValidate())->goCheck();
        if(false == $model->save($post)) {
          return error('添加失败');
        } else {
          adminLog();//写入日志
          return success('添加成功','admin/Route/index');
        }
      } else {
        //非提交操作
        return View::fetch();
      }
    }
  }
  
  // 路由开关
  public function status()
  {
    //获取文件id
    $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    if($id > 0) {
    if($this->request->isPost()) {
      //是提交操作
      $post = $this->request->post();
      $status = $post['status'];
      if(false == RouteModel::where('id',$id)->save(['status'=>$status])) {
        return error('设置失败');
      } else {
        adminLog();//写入日志
        return success('设置成功','admin/route/index');
      }
    } 
    } else {
      return $this->error('id不正确');
    }
  }

  // 删除
  public function delete()
  {
    if($this->request->isAjax()) {
      $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
      if(false == RouteModel::where('id',$id)->delete()) {
        return error('删除失败');
      } else {
        adminLog($id);//写入日志
        return success('删除成功','admin/route/index');
      }
    }
  }

}

?>